using System;
using System.Globalization;
using Windows.Globalization.NumberFormatting;
using Windows.UI.Xaml.Data;

namespace WinRTConverters.Format
{
    public class DoubleToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null)
            {
                var decimalFormatter = new DecimalFormatter(new string[] { CultureInfo.CurrentCulture.Name }, "ZZ");
                return decimalFormatter.FormatDouble((double)value);
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            var decimalFormatter = new DecimalFormatter(new string[] { CultureInfo.CurrentCulture.Name }, "ZZ");
            var d = decimalFormatter.ParseDouble(value.ToString());
            return d != null ? (double)d : 0;
        }
    }
}
